package cn.tkb.org.service.impl;

import cn.tkb.basic.exception.BusinessException;
import cn.tkb.basic.service.impl.BaseServiceImpl;
import cn.tkb.org.domain.Employee;
import cn.tkb.org.domain.Shop;
import cn.tkb.org.mapper.EmployeeMapper;
import cn.tkb.org.mapper.ShopMapper;
import cn.tkb.org.service.IEmployeeService;
import cn.tkb.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IEmployeeService employeeService;
    @Override
    @Transactional
    //店铺入驻
    public void settlement(Shop shop) {
        //校验
        //校验店铺名称不能为空
        if(shop.getName()==null) {
            throw new BusinessException("店铺名称不能为空");
        }
        Employee admin = shop.getAdmin();
        //1.账号是否被注册 employee 去校验username phone emial 三个字段
        Employee user = employeeMapper.loadByUser(admin.getUsername());
        if(user!=null){
            throw new BusinessException("管理员已被注册，请直接登录，如果忘记密码，请找回密码！！！");
        }
        //1.校验两次密码是否一致 如果不一致抛出业务异常
        if(!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("密码不一致请重新输入密码！！");
        }
            //入驻
            //1.保存员工信息，并返回自增长id
        employeeService.add(admin);
            //2.保存店铺信息
        shop.setAdmin(admin);
        shopMapper.save(shop);
            //3.修改员工所属店铺id
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);

    }
}
